package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.ShopSearchQueryObject;
import cn.wolfcode.luowowo.search.repository.IShopDetailTemplateRepository;
import cn.wolfcode.luowowo.search.service.IShopDetailTemplateSearchService;
import cn.wolfcode.luowowo.search.template.ShopDetailTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.sun.corba.se.spi.ior.IdentifiableFactory;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopDetailTemplateSearchServiceImpl implements IShopDetailTemplateSearchService {

    @Autowired
    private IShopDetailTemplateRepository repository;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    //添加操作
    @Override
    public void saveOrUpdate(ShopDetailTemplate shopDetailTemplate) {

        repository.save(shopDetailTemplate);


    }

    @Override
    public List<Map<String, Object>> selectByThemeId(int type) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //查到指定主题id为特定值的
        NativeSearchQueryBuilder parentId = builder.withQuery(QueryBuilders.termQuery("themeId", type));

        Page<ShopDetailTemplate> search = repository.search(parentId.build());
        //查到指定主题id为特定值的
        List<ShopDetailTemplate> list = search.getContent();

        //找StaticVO取需要数据
        List<Map<String, Object>> vos = new ArrayList<>();

        for (ShopDetailTemplate st : list) {
//            通过父类id找到子类集合
            List<StatisVO> statise = getDestByParentId(st.getId());
            StatisVO vo = new StatisVO();
            vo.setId(st.getId());
            vo.setName(st.getTitle());

            Map<String, Object> map = new HashMap<>();
            map.put("parent", vo);
            map.put("sons", statise);

            //封装到集合;
            vos.add(map);
        }
        System.out.println(vos);
        return vos;
    }
    //查询子菜单
    @Override
    public List<StatisVO> selectByParentIdIsNull() {
        Iterable<ShopDetailTemplate> iterable = repository.search(QueryBuilders.existsQuery("parentId"));
        List<StatisVO> list = new ArrayList<>();

        //第三个完善:目的地去除重复
        List<String> temp = new ArrayList<>(); //放置已经存在的目的地名称
        iterable.forEach(st -> {

            //如果临时的list中不存在这个目的地名,执行添加操作
            if (!temp.contains(st.getTitle())) {
                StatisVO vo = new StatisVO();
                vo.setId(st.getId());
                vo.setName(st.getTitle());
                list.add(vo);

                //添加完成之后,临时list必须将该目的地名保存
                temp.add(st.getTitle());
            }
        });

        return list;
    }


    //通过子菜单找到相应的下商品
    @Override
    public Page query(ShopSearchQueryObject qo) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (qo.getType()==null){
            qo.setType(-1);
        }
        if (qo.getType()==-1){
            boolQuery.must(QueryBuilders.existsQuery("id"));//默认查全部
        }else if (qo.getType()==ShopSearchQueryObject.SHOP_TYPE_THEME){
            boolQuery.must(QueryBuilders.termQuery("strategyId", qo.getTypeValue()));//找子菜单下的所有商品
        }else if (qo.getType()==ShopSearchQueryObject.SHOP_TYPE_UN_ROLL){
            boolQuery.must(QueryBuilders.termQuery("id", qo.getTypeValue()));//查指定热销商品
        }

        return repository.search(boolQuery,qo.getPageable());
    }

    //通过父类id的扎到相应的Id和名字
    private List<StatisVO> getDestByParentId(Long parentId) {
        Iterable<ShopDetailTemplate> iterable = repository.search(QueryBuilders.termQuery("parentId", parentId));
        List<StatisVO> list = new ArrayList<>();

        //第三个完善:目的地去除重复
        List<String> temp = new ArrayList<>(); //放置已经存在的目的地名称
        iterable.forEach(st -> {

            //如果临时的list中不存在这个目的地名,执行添加操作
            if (!temp.contains(st.getTitle())) {
                StatisVO vo = new StatisVO();
                vo.setId(st.getId());
                vo.setName(st.getTitle());
                list.add(vo);

                //添加完成之后,临时list必须将该目的地名保存
                temp.add(st.getTitle());
            }
        });

        return list;
    }

}
